package com.bugsnag.ohos;

import com.bugsnag.ohos.util.OhosLog;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Supplies the headers which must be used in any request sent to the Error Reporting API.
 *
 * @since 2021-07-02
 */
public class DeliveryHeaders {
    /**
     * HEADER_BUGSNAG_INTEGRITY
     */
    public static final String HEADER_BUGSNAG_INTEGRITY = "Bugsnag-Integrity";
    /**
     * HEADER_API_KEY
     */
    public static final String HEADER_API_KEY = "Bugsnag-Api-Key";
    /**
     * HEADER_INTERNAL_ERROR
     */
    public static final String HEADER_INTERNAL_ERROR = "Bugsnag-Internal-Error";
    private static final String HEADER_API_PAYLOAD_VERSION = "Bugsnag-Payload-Version";
    private static final String HEADER_BUGSNAG_SENT_AT = "Bugsnag-Sent-At";
    private static final String HEADER_BUGSNAG_STACKTRACE_TYPES = "Bugsnag-Stacktrace-Types";
    private static final String HEADER_CONTENT_TYPE = "Content-Type";

    private DeliveryHeaders() {
    }

    /**
     * errorApiHeaders
     *
     * @param payload
     * @return errorApiHeaders
     */
    public static Map<String, String> errorApiHeaders(EventPayload payload) {
        OhosLog.d("DeliveryHeaders", "errorApiHeaders start...%s ", payload);
        Map<String, String> mutableHeaders = new HashMap<>();
        mutableHeaders.put(HEADER_API_PAYLOAD_VERSION, "4.0");
        String apiKey = payload.apiKey;
        if (apiKey == null) {
            apiKey = "";
        }
        mutableHeaders.put(HEADER_API_KEY, apiKey);
        mutableHeaders.put(HEADER_BUGSNAG_SENT_AT, DateUtils.toIso8601(new Date()));
        mutableHeaders.put(HEADER_CONTENT_TYPE, "application/json");

        Set<ErrorType> errorTypes = payload.getErrorTypes();
        OhosLog.d("DeliveryHeaders", "errorTypes :%s", errorTypes);
        if (!errorTypes.isEmpty()) {
            mutableHeaders.put(HEADER_BUGSNAG_STACKTRACE_TYPES, serializeErrorTypeHeader(errorTypes));
        }
        OhosLog.d("DeliveryHeaders", "errorApiHeaders end...");
        return mutableHeaders;
    }

    /**
     * Serializes the error types to a comma delimited string
     *
     * @param errorTypes
     * @return error type header
     */
    public static String serializeErrorTypeHeader(Set<ErrorType> errorTypes) {
        if (errorTypes.isEmpty()) {
            return "";
        } else {
            String desc = "";
            for (ErrorType errorType : errorTypes) {
                if (desc.length() > 0) {
                    desc = desc + "," + errorType.desc;
                } else {
                    desc = errorType.desc;
                }
            }
            return desc;
        }
    }

    /**
     * Supplies the headers which must be used in any request sent to the Session Tracking API.
     *
     * @param apiKey
     * @return the HTTP headers
     */
    public static Map<String, String> sessionApiHeaders(String apiKey) {
        Map<String, String> mutableHeaders = new HashMap<>();
        mutableHeaders.put(HEADER_API_PAYLOAD_VERSION, "1.0");
        String key = apiKey;
        if (key == null) {
            key = "";
        }
        mutableHeaders.put(HEADER_API_KEY, key);
        mutableHeaders.put(HEADER_BUGSNAG_SENT_AT, DateUtils.toIso8601(new Date()));
        mutableHeaders.put(HEADER_CONTENT_TYPE, "application/json");
        return mutableHeaders;
    }
}
